package com.ctrip.study.algorithm.sort;

import java.util.Arrays;

import org.junit.Test;

import junit.framework.TestCase;

public class MergeSort extends TestCase {

	public void sortReady() {
		int[] arrayA = { 23, 47, 81, 95 };
		int[] arrayB = { 7, 14, 39, 55, 62, 74 };
		int[] arrayC = new int[arrayA.length + arrayB.length];
		
		int aIndex = 0, bIndex = 0, cIndex = 0;
		
		for (int i = 0; i < arrayC.length; i++) {
			if(arrayA[aIndex] < arrayB[bIndex]){
				arrayC[cIndex] = arrayA[aIndex];
				if(aIndex < arrayA.length - 1){
					aIndex++;
				}else{
					for (int j = arrayB.length - bIndex; j > 0; j--) {
						cIndex++;
						arrayC[cIndex] = arrayB[bIndex];
						bIndex++;
					}
					break;
				}
			}
			else if(arrayB[bIndex] < arrayA[aIndex]){
				arrayC[cIndex] = arrayB[bIndex];
				if(bIndex < arrayB.length - 1){
					bIndex++;
				}else{
					for (int j = arrayA.length - aIndex; j > 0; j--) {
						cIndex++;
						arrayC[cIndex] = arrayA[aIndex];
						aIndex++;
					}
					break;
				}
			}
			cIndex++;
		}
		System.out.println(Arrays.toString(arrayC));
	}

	
	

	@Test
	public void testSortReady() {
		sortReady();
	}

}
